﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AGuang.WebTool {
    public class NumberTool {
        /// <summary>
        /// 将罗马数字转换为阿拉伯数字
        /// </summary>
        /// <param name="roman">罗马数字</param>
        /// <returns></returns>
        public static int ConvertRoman(String roman) {
            roman = roman.ToUpper();
            roman.Replace("_V", "S");
            roman.Replace("_X", "R");
            roman.Replace("_L", "Q");
            roman.Replace("_C", "P");
            roman.Replace("_D", "O");
            roman.Replace("_M", "N");

            int arabic = 0, sidx = 0;
            int len = roman.Length - 1;
            char[] data = roman.ToCharArray();

            int oldValue = 0;
            while (len >= 0) {
                int i = 0;
                sidx = len;
                int value = ConvertRoman2(data[sidx]);
                if (value < oldValue) arabic -= value;
                else arabic += value;
                oldValue = value;
                i++;
                len--;
            }

            return arabic;
        }

        /// <summary>  

        /// 将中文数字转换成阿拉伯数字  

        /// </summary>  

        /// <param name="cnNumber"></param>  

        /// <returns></returns>  

        public static int ConverCnToDigit(string cnNumber) {
            int result = 0;
            int temp = 0;
            foreach (char c in cnNumber) {
                int temp1 = ConvertCnToDigit2(c);
                if (temp1 == 10000) {
                    result += temp;
                    result *= 10000;
                    temp = 0;
                } else if (temp1 > 9) {
                    if (temp1 == 10 && temp == 0) temp = 1;
                    result += temp * temp1;
                    temp = 0;
                } else temp = temp1;
            }
            result += temp;
            return result;
        }



        /// <summary>  
        /// 将中文数字转换成阿拉伯数字  
        /// </summary>  
        /// <param name="cn"></param>  
        /// <returns></returns>  
        static int ConvertCnToDigit2(char cn) {
            int number = 0;
            switch (cn) {
                case '壹':
                case '一':
                    number = 1;
                    break;
                case '两':
                case '贰':
                case '二':
                    number = 2;
                    break;
                case '叁':
                case '三':
                    number = 3;
                    break;
                case '肆':
                case '四':
                    number = 4;
                    break;
                case '伍':
                case '五':
                    number = 5;
                    break;
                case '陆':
                case '六':
                    number = 6;
                    break;
                case '柒':
                case '七':
                    number = 7;
                    break;
                case '捌':
                case '八':
                    number = 8;
                    break;
                case '玖':
                case '九':
                    number = 9;
                    break;
                case '拾':
                case '十':
                    number = 10;
                    break;
                case '佰':
                case '百':
                    number = 100;
                    break;
                case '仟':
                case '千':
                    number = 1000;
                    break;
                case '萬':
                case '万':
                    number = 10000;
                    break;
                case '零':
                default:
                    number = 0;
                    break;
            }
            return number;
        }



        /// <summary>  
        /// 将中文数字转换成阿拉伯数字  
        /// </summary>  
        /// <param name="cnDigit"></param>  
        /// <returns></returns>  
        public static long ConvertCnToToLong(string cnDigit) {
            long result = 0;
            string[] str = cnDigit.Split('亿');
            result = ConverCnToDigit(str[0]);
            if (str.Length > 1) {
                result *= 100000000;
                result += ConverCnToDigit(str[1]);
            }
            return result;
        }

        /// <summary>
        /// 错误百出啊,用于转100以内的数还是不错的
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static String ConvertToChinese(uint num) {
            if (num == 0) return "零";
            String numStr="零一二三四五六七八九";
            String[] jz = { "", "十", "百", "千" };
            String[] jz2 = { "", "万", "亿" };
            bool preIsZero = false;
            bool isStart = true;
            StringBuilder sb = new StringBuilder();
            int i = 0;
            while (true) {
               
                uint wei = num % 10;
                num = num / 10;
                if (isStart && wei == 0) {

                } else if (preIsZero && wei == 0) {

                } else {
                    if (i % 4 == 0) sb.Insert(0, jz2[i / 4]);
                    sb.Insert(0, jz[i % 4]);
                    if (num != 0 || wei != 1 || i % 4 != 1) sb.Insert(0, numStr[(int)wei]);
                    
                    if (wei == 0) preIsZero = true;
                    else preIsZero = false;
                }
                i++;
                if (num == 0) break;
            }
            return sb.ToString();
        }


        /// <summary>
        /// 罗马数字转换基础
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        private static int ConvertRoman2(char c) {
            switch (c) {
                case 'I':
                    return 1;
                case 'V':
                    return 5;
                case 'X':
                    return 10;
                case 'L':
                    return 50;
                case 'C':
                    return 100;
                case 'D':
                    return 500;
                case 'M':
                    return 1000;
                case 'S':
                    return 5000;
                case 'R':
                    return 10000;
                case 'Q':
                    return 50000;
                case 'P':
                    return 100000;
                case 'O':
                    return 500000;
                case 'N':
                    return 1000000;
                default:
                    return 0;
            }
        }
    }
}
